﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Xml;
using System.Net;
using System.Windows.Media;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Security;
using System.Collections.Concurrent;
using Itasa.API;
using Itasa.RSS;

namespace Itasa
{
    internal static class Params
    {
        static internal List<Show> Shows;
        static internal List<Sub> LastSubs;
        static internal List<Episode> Episodes;
        static internal Calendar Calendario;
        static internal List<Torrent_Show> TorrentShows;
        static internal ITASA_user user;
        static internal MainWindow mW = null;
        static internal Config cfg = null;
        static internal List<RSS_Sub> myITASA = null;
        static internal List<RSS_Sub> ITASA = null;
        static internal Download_Manager dm = new Download_Manager();
        static internal Thread dm_t;
        static internal Thread updater;
        static internal Thread logUpdater;
        static internal int updatems = 300000;
        static internal ConcurrentStack<String> logStack = new ConcurrentStack<String>();
        static internal AutoResetEvent logStack_event = new AutoResetEvent(false);

        static internal String cleanPathName(String s)
        {
            List<Char> invalid = new List<char>(Path.GetInvalidPathChars());
            invalid.AddRange(Path.GetInvalidFileNameChars());
            /*Console.WriteLine("Invalids");
            ShowChars(invalid);
            Console.WriteLine();*/

            String newS = "";
            foreach (char c in s)
            {
                bool ok = true;
                foreach (char inv in invalid) if (inv == c) ok = false;
                if (ok) newS += c;
                else newS+=' ';
            }
            while (newS[newS.Length - 1] == ' ') newS = newS.Remove(newS.Length - 1);
            return newS;
        }

        private static void ShowChars(List<char> charArray)
        {
            Console.WriteLine("Char\tHex Value");
            // Display each invalid character to the console.
            foreach (char someChar in charArray)
            {
                if (Char.IsWhiteSpace(someChar))
                {
                    Console.WriteLine(",\t{0:X4}", (int)someChar);
                }
                else
                {
                    Console.WriteLine("{0:c},\t{1:X4}", someChar, (int)someChar);
                }
            }
        }

        static internal String getDestPath(SubInfo info)
        {
            if (Config.IsShowDir) return Config.SubsDir + @"\" + cleanPathName(info.ShowName) + @"\";
            else return Config.SubsDir + @"\";
        }

        static internal String getFilePath(SubInfo info)
        {
            if (Config.IsShowDir) return Config.SubsDir + @"\" + cleanPathName(info.ShowName) + @"\" + cleanPathName(info.FileName);
            else return Config.SubsDir + @"\" + cleanPathName(info.FileName);
        }

        static internal void writeLog(String s)
        {
            logStack.Push(s);
            logStack_event.Set();
        }

        static internal Show SearchShow(long ID)
        {
            if (Shows != null) foreach (Show s in Shows) if (s.ID == ID) return s;
            return null;
        }

        static internal void Dispatch(Control called, Action f)
        {
            if (called.Dispatcher.CheckAccess()) f();
            else called.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate() { f(); });
        }

        static internal void Dispatch(Control called, Action<double, String> f, double p1, String p2)
        {
            if (called.Dispatcher.CheckAccess()) f(p1, p2);
            else called.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate() { f(p1, p2); });
        }

        static internal void Dispatch<T>(Control called, Action<List<T>> f, List<T> p)
        {
            if (called.Dispatcher.CheckAccess()) f(p);
            else called.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate() { f(p); });
        }

    }
}
